package threadlocal;

import java.util.concurrent.TimeUnit;

/**
 * @author chaird
 * @create 2021-05-14 21:51
 */
public class InheritableThreadLocalDemo {
  public static void main(String[] args) throws Exception {

    InheritableThreadLocal threadLocal = new InheritableThreadLocal();
    threadLocal.set(1);

    System.out.println("父线程拿到的数据："+ threadLocal.get());

    new Thread(
            () -> {
             while (true){
                 try { TimeUnit.SECONDS.sleep(1); } catch (Exception e) { e.printStackTrace(); } finally { }
                 System.out.println("子线程拿到的数据："+ threadLocal.get());
             }
            },
            "child-01")
        .start();

      threadLocal.set(2);
      System.out.println("父线程拿到的数据："+ threadLocal.get());

  }
}
